$(function() {
    var e = $("#assessment").val();
    $("#count_size").html(1000 - parseInt(e.length));
    $(document).on("focus", "input,textarea,select", function(e) {
//        $(this).parent().parent().siblings().children().children().removeClass("green")
        $(":input").removeClass("green");
        $(this).addClass("green");
    });
    $(document).on("click", '.fhqx', function(e) {
        var step = $(this).attr('step');
        $(".biaodan" + step).hide();
        $(".neirong" + step).show();
    })
    $(document).on("click", ".add1", function(e) {

        var num = $(this).attr("num");
        var step = $(this).attr('step');
        $(".biaodan" + step).show();
        $(".neirong" + step).hide();
        $.ajax({
            url: "/User/Index/s2html",
            data: "num=" + num + "&step=" + step,
            type: "post",
            dataType: "json",
            success: function(ret) {

                $(".s" + step + "html").html(ret.info);
            }

        });


    });
    $(document).on("click", '.edit', function(e) {
        var step = $(this).attr('step');
        var num = $(this).attr('num');
        $(".biaodan" + step).show();
        $(".neirong" + step).hide();
        if (num)
        {
            $.ajax({
                url: "/User/Index/s2html",
                data: "num=" + num + "&step=" + step,
                type: "post",
                dataType: "json",
                success: function(ret) {
                    $(".s" + step + "html").html(ret.info);
                    //
                }

            });
        }
        else
        {
            $(".text" + step).hide();
            $(".post" + step).show();
        }
    });
    $(document).on("click", "#pereonal", function(e) {
        var step = $(this).attr('step');

        var ptc = $(this).attr('ptc');
        var num = $(this).attr('num');

        if (step == 1)
        {
            var first_name = getValue("first_name");
            var last_name = getValue("last_name");
            if (first_name == '')
                return alert('请填写First Name！');
            if (last_name == '')
                return alert('请填写Last Name！');

            var email = checkemail($("input[name='email']").val());
            var address = $("#address").val();
            var mob = checkmobile($("input[name='mobile']").val());
            var str = "&first_name=" + first_name + "&last_name=" + last_name + "&email=" + email + "&address=" + address + "&mob=" + mob;
        }
        if (step == 2)
        {
            var objective = $("#objective").val();
            if (objective == '' || objective == 'Your expected job position or career goal etc. For example,Seeking the online English teacher position at XXX company')
                return alert('请填写OBJECTIVE！');

            var str = "&objective=" + objective;
        }
        if (step == 3)
        {

            var school = $(this).parent().parent().children().children('#school').val();
            if (school == '')
                return alert('请填写School！');
            var degree = $(this).parent().parent().children().children('#degree').val();
            if (degree == '')
                return alert('请填写Degree！');
            if (num)
            {
                var str = $("#form" + step + '-' + num).serialize();
                str = "&num=" + num + "&" + str;
            }
            else
            {
                var str = $("#form" + step).serialize();
                str = "&" + str;
            }


        }
        if (step == 4)
        {
            var company = $("#company").val();
            if (company == '')
                return alert('请填写Company Name！');
            var position = $("#position").val();
            if (position == '')
                return alert('请填写Position！');
            var month1 = $("input[name='month1']").val();
            if (month1 == '')
                return alert('请填写开始月份！');
            var year1 = $("input[name='year11']").val();
            if (year1 == '')
                return alert('请填写开始年份1！');
            var month2 = $("input[name='month2']").val();
            if (month2 == '')
                return alert('请填写截止月份！');
            var year2 = $("input[name='year22']").val();
            if (year2 == '')
                return alert('请填写截止年份！');
            if (year1 > year2) {
                return alert("结束时间必须大于开始时间");
            } else if (year1 == year2) {
                if (month1 >= month2) {
                    return alert('结束时间必须大于开始时间');
                }
            }

            var description1 = $("#description1").val();
            if (description1 == '')
                return alert('请填写Description！');

            if (num)
            {
                var str = $("#form" + step + '-' + num).serialize();
                str = "&num=" + num + "&" + str;
            }
            else
            {
                var str = $("#form" + step).serialize();
                str = "&" + str;
            }



        }
        if (step == 5)
        {

            //var str = $("#form"+step).serialize();
            str = '';
            var i = 1;
            $("input[name=lang]").each(function() {
                str += '&lang' + i + '=' + $(this).val();
                i++;
            });
            i = 1;
            $("input[name=level]").each(function() {
                str += '&level' + i + '=' + $(this).val();
                i++;
            });

        }
        if (step == 6)
        {
            var assessment = $("#assessment").val();
            if (assessment.length > 1000) {
                return alert("字数必须在一千字以内");
            }
            if (assessment == '' || assessment == 'Please tell us something about yourself, such as:characher、hobb etc')
                return alert('请填写ASSESSMENT！');

            var str = "&assessment=" + assessment;

        }
        $.ajax({
            url: "/User/Index/postResume",
            data: "step=" + step + "&precentum=" + ptc + str,
            type: "post",
            dataType: "json",
            success: function(ret) {
                if (ret.status == 0)
                    return alert($ret.info);
                if (ret.status == 1)
                {
                    alert('编辑成功！');
                    //$(".text"+step).show();
                    //$(".post"+step).hide();
                    //$(".con"+step).html();
                    location.reload();
                }
            }

        });


    });
    $(document).on("click", '.sel', function(e) {
        $(this).children(".lieb").slideToggle(200);
    });
    $(document).on("click", '.sell', function(e) {
        $(this).children(".llieb").slideToggle(200);
    });
    $(document).on("click", '.dellang', function(e) {
        $(this).parent().remove();
    });
    $(document).on("click", '.lieli', function(e) {
        var html = $(this).html();
        $(this).parent(".lieb").parent(".sel").children(".selinput").html(html);
        $(this).parent(".lieb").children("#year").val(html);
        $(this).parent(".llieb").parent(".sell").children(".sellinput").html(html);
        $(this).parent(".llieb").children("#year").val(html);
    });

    $(document).on("click", '.del', function(e) {
        var step = $(this).attr('step');
        var num = $(this).attr('num');
        if (confirm("是否删除")) {
            $.ajax({
                url: "/User/Index/delResume",
                data: "step=" + step + "&num=" + num,
                type: "post",
                dataType: "json",
                success: function(ret) {
                    if (ret.status == 0)
                        return alert($ret.info);
                    if (ret.status == 1)
                    {
                        alert('删除成功！');
                        //$(".text"+step).show();
                        //$(".post"+step).hide();
                        //$(".con"+step).html();
                        location.reload();
                    }
                }

            });
        }

    });
    $(document).on("click", '.add', function(e) {

        var num = $(this).attr('anum');
        $(this).attr('anum', parseInt(num) + 1);
        $.ajax({
            url: "/User/Index/postLang",
            data: "anum=" + num,
            type: "post",
            dataType: "json",
            success: function(ret) {
                $(".biaodan5").append(ret.info);
            }

        });

    });
    $(document).on("click", '.qx', function(e) {
        var step = $(this).attr('step');
        var num = $(this).attr('num');
        if (num)
        {
            var step = $(this).attr('step');
            $(".biaodan" + step).hide();
            $(".neirong" + step).show();

        }
        else
        {
            $(".text" + step).show();
            $(".post" + step).hide();
        }
    })



    $(document).on("focus", 'input', function(e) {
        var v = $(this).attr('v');
        var val = $(this).val();
        if (val == v)
            $(this).val('');
    })
    $(document).on("blur", 'input', function(e) {
        var v = $(this).attr('v');
        var val = $(this).val();
        if (val == '')
            $(this).val(v);
    })
    $(document).on("focus", '#objective', function(e) {
        var objective = $('#objective').val();
        if (objective == 'Your expected job position or career goal etc. For example,Seeking the online English teacher position at XXX company') {
            $(this).html("");
        }

    })
    $(document).on("blur", '#objective', function(e) {
        var objective = $('#objective').val();
        if (objective == '') {
            $(this).html("Your expected job position or career goal etc. For example,Seeking the online English teacher position at XXX company");
        }
    })
    $(document).on("blur", "input,textarea,select", function(e) {
        var value = $(this).val();
        if (value == '') {
            $(this).siblings('div').show();
        }
        var name = $("input[name='name']").val();
        var email = $("input[name='email']").val();
        var address = $("input[name='address']").val();
        var mob = $("input[name='mobile']").val();
        if (name != '') {
            $("input[name='name']").siblings('div').hide();
        }
        if (mob != '') {
            var mobile = mob.match(/^1[3|4|5|8][0-9]\d{4,8}$/);
            if (mobile) {
                $("#tishi_mobile").hide();
            } else {
                $("#tishi_mobile").html("请填写有效的11位手机号码！")
                $("#tishi_mobile").show();
            }
        } else {
            $("#tishi_mobile").hide();
        }
        if (email != '') {
            var flag = email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
            if (flag) {
                $("#tishi_email").hide();
            } else {
                $("#tishi_email").html("请填写有效的邮箱地址！")
                $("#tishi_email").show();
            }
        } else {
            $("#tishi_email").hide();
        }
    });
})
//获取值 并处理值
function getValue(con)
{
    var v = $("#" + con).attr('v');
    var va = $("#" + con).val();
    if (v == va || va == '')
    {
        return '';
    }
    else
    {
        return va;
    }
}
function checkmobile(mobile) {
    if (mobile) {
        if (mobile.match(/^1[3|4|5|8][0-9]\d{4,8}$/)) {
            return mobile;
        } else {
            return false;
        }
    } else {
        return mobile;
    }
}
function checkemail(email) {
    if (email) {
        if (email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)) {
            return email;
        } else {
            return false;
        }
    } else {
        return email;
    }


}
//打印
function printResume(printpage) {
    var headstr = "<html><head><title></title></head><body>";
    var footstr = "</body>";
    var newstr = document.getElementById(printpage).innerHTML;
    var oldstr = document.body.innerHTML;
    $('body').addClass('body');
    document.body.innerHTML = headstr + newstr + footstr;
    window.print();
    document.body.innerHTML = oldstr;
    return false;
}

//下载
function downResume(printpage) {
    var headstr = "<html><head><title></title></head><body>";
    var footstr = "</body>";
    var newstr = document.getElementById(printpage).innerHTML;
    var oldstr = document.body.innerHTML;
    $('body').addClass('body');
    document.body.innerHTML = headstr + newstr + footstr;
    return false;
}
